package net.ewon.avatar.common.pkg;

import javax.xml.bind.annotation.XmlRootElement;

import org.codehaus.jackson.annotate.JsonIgnore;

@XmlRootElement(name="Version")
public class Version {

	private int major;
	private int minor;
	private int decimal;

	public Version() {
	} 

	public Version(int major, int minor, int decimal) {
		this.major = major;
		this.minor = minor;
		this.decimal = decimal;
	}

	public static Version parse(String version) {
		if (version == null) {
			throw new IllegalArgumentException("The version can't be null");
		}

		String[] ver = version.split("\\.");
		
		if (ver.length!=3) {
			throw new IllegalArgumentException("The version " + version + " is not valid.");
		}
		
		return new Version(Integer.parseInt(ver[0]), Integer.parseInt(ver[1]),
				Integer.parseInt(ver[2]));
	}

	@JsonIgnore
	public String getVersion() {
		return major + "." + minor + "." + decimal;
	}

	public int getMajor() {
		return major;
	}

	public void setMajor(int major) {
		this.major = major;
	}

	public int getMinor() {
		return minor;
	}

	public void setMinor(int minor) {
		this.minor = minor;
	}

	public int getDecimal() {
		return decimal;
	}

	public void setDecimal(int decimal) {
		this.decimal = decimal;
	}

	public String toString() {
		return getVersion();
	}
}
